--动作管理类
--mgrs.action:beginAction(self.label):MoveTo(2,cc.p(200,200)):ScaleTo(2,0.1):endAction();
--


local ActionMgr  = class("ActionMgr");

function ActionMgr:ctor()
    self.actions = {};
    self.node = nil;
    self.actionTag = -1;
end

function ActionMgr:beginAction(node)
    self.actions = {};
    self.node = node;
    return self;
end

function ActionMgr:setActionTag(tag)
    self.actionTag = tag;
    return self;
end

function ActionMgr:AddOther(action)
    table.insert(self.actions,action);
    return self;
end

function ActionMgr:Clone()
    local index = table.getn(self.actions);
    local action = self.actions[index];
    local newAction = action:clone();
    table.insert(self.actions,newAction);
    return self;
end

function ActionMgr:Reverse()
    local index = table.getn(self.actions);
    local action = self.actions[index];
    local newAction = action:reverse();
    table.insert(self.actions,newAction);
    return self;
end

function ActionMgr:Sequence(count)
    if count < 2 or #self.actions < 2 then
        return self;
    end
    assert(count <= #self.actions,"arguments error:count is large to the length of actions tables");
    local tmpActions = {};
    for i = #self.actions, #self.actions - count + 1, -1  do
        table.insert(tmpActions,1,self.actions[i]);
        table.remove(self.actions,i);
    end
    local seq = self:getEndSeqActions(tmpActions);
    table.insert(self.actions,seq);
    return self;
end

function ActionMgr:Spawn(count)
	if count < 2 or #self.actions < 2 then
        return self;
    end
    assert(count <= #self.actions,"arguments error:count is large to the length of actions tables");
    local tmpActions = {};
    for i = #self.actions, #self.actions - count + 1, -1 do
        table.insert(tmpActions,1,self.actions[i]);
        table.remove(self.actions,i);
    end
    local spawn = self:getEndSpawn(tmpActions);
    table.insert(self.actions,spawn);
    return self;
	
end

function ActionMgr:Repeat(times)
    local index = table.getn(self.actions);
    local action = self.actions[index];
    table.remove(self.actions,index);
    table.insert(self.actions,cc.Repeat:create(action,times));
    return self;
end

function ActionMgr:RepeatForever()
    local index = table.getn(self.actions);
    local last = self.actions[index];
    table.remove(self.actions,index);
    table.insert(self.actions,cc.RepeatForever:create(last));
	return self;
end


function ActionMgr:MoveTo(s,pos)
    table.insert(self.actions,cc.MoveTo:create(s,pos));
    return self;
end

function ActionMgr:MoveBy(s,pos)
    table.insert(self.actions,cc.MoveBy:create(s,pos));
    return self;
end

function ActionMgr:ScaleTo(s,scale)
    table.insert(self.actions,cc.ScaleTo:create(s,scale));
    return self;
end

function ActionMgr:ScaleBy(s,scale)
    table.insert(self.actions,cc.ScaleBy:create(s,scale));
    return self;
end

function ActionMgr:FadeOut(t)
    table.insert(self.actions,cc.FadeOut:create(t));
	return self;
end

function ActionMgr:FadeIn(t)
    table.insert(self.actions,cc.FadeIn:create(t));
	return self;
end

function ActionMgr:FadeTo(t,num)
	table.insert(self.actions,cc.FadeTo:create(t,num));
	return self;
end

function ActionMgr:RotateBy(time,angle)
    table.insert(self.actions,cc.RotateBy:create(time,angle));
    return self;
end

function ActionMgr:RotateTo(time,angle)
    table.insert(self.actions,cc.RotateTo:create(time,angle));
    return self;
end

function ActionMgr:JumpTo(time,point,high,count)
    table.insert(self.actions,cc.JumpTo:create(time,point,high,count));
    return self;
end

function ActionMgr:JumpBy(time,point,high,count)
    table.insert(self.actions,cc.JumpBy:create(time,point,high,count));
    return self;
end

function ActionMgr:BezierTo(time,points)
   table.insert(self.actions,cc.BezierTo:create(time,points));
   return self;
end

function ActionMgr:BezierBy(time,points)
    table.insert(self.actions,cc.BezierBy:create(time,points));
    return self;
end

function ActionMgr:DelayTime(time)
    table.insert(self.actions,cc.DelayTime:create(time));
    return self;
end

function ActionMgr:CallFunc(func)
    table.insert(self.actions,cc.CallFunc:create(func));
    return self;
end


function ActionMgr:EaseIn(target)
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseIn:create(target,last);
	return self;
end


function ActionMgr:EaseOut(target)
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseOut:create(target,last);
	return self;
end


function ActionMgr:EaseInOut(target)
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseInOut:create(target,last);
	return self;
end


function ActionMgr:TargetedAction(target)
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.TargetedAction:create(target,last);
	return self;
end


function ActionMgr:EaseExponentialIn()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseExponentialIn:create(last);
	return self;
end


function ActionMgr:EaseExponentialOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseExponentialOut:create(last);
	return self;
end

function ActionMgr:EaseExponentialInOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseExponentialInOut:create(last);
	return self;
end


function ActionMgr:EaseSineIn()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseSineIn:create(last);
	return self;
end


function ActionMgr:EaseSineOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseSineOut:create(last);
	return self;
end


function ActionMgr:EaseSineInOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseSineInOut:create(last);
	return self;
end

function ActionMgr:EaseElastic()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseElastic:create(last);
	return self;
end

function ActionMgr:EaseElasticIn()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseElasticIn:create(last);
	return self;
end

function ActionMgr:EaseElasticOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseElasticOut:create(last);
	return self;
end

function ActionMgr:EaseElasticInOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseElasticInOut:create(last);
	return self;
end

function ActionMgr:EaseBounce()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseBounce:create(last);
	return self;
end

function ActionMgr:EaseBounceIn()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseBounceIn:create(last);
	return self;
end

function ActionMgr:EaseBounceOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseBounceOut:create(last);
	return self;
end

function ActionMgr:EaseBounceInOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseBounceInOut:create(last);
	return self;
end

function ActionMgr:EaseBackIn()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseBackIn:create(last);
	return self;
end

function ActionMgr:EaseBackOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseBackOut:create(last);
	return self;
end

function ActionMgr:EaseBackInOut()
    local last = self.actions[#self.actions];
    self.actions[#self.actions] = cc.EaseBackInOut:create(last);
	return self;
end

function ActionMgr:Show()
    table.insert(self.actions,cc.Show:create());
    return self;
end

function ActionMgr:Hide()
    table.insert(self.actions,cc.Hide:create());
    return self;
end

function ActionMgr:ToggleVisibility()
    table.insert(self.actions,cc.ToggleVisibility:create());
    return self;
end

function ActionMgr:RemoveSelf()
    table.insert(self.actions,cc.RemoveSelf:create());
    return self;
end

function ActionMgr:PlayAnimate(name,isForever)
    local animate = mgrs.animate:getAnimate(name);
    isForever = isForever or false;
    local action = animate;
    if isForever then
        action = cc.RepeatForever:create(animate);
    end
    table.insert(self.actions,action);
    return self;
end

function ActionMgr:endAction()
    assert(not tolua.isnull(self.node), "ActionMgr.endAction - node is nil")
    local action = self:getEndSeqActions(self.actions);
    if self.actionTag ~= -1 then
        action:setTag(self.actionTag);
    end
    self.node:runAction(action);
end

function ActionMgr:getEndSeqActions(actions)
    if #actions < 1 then return end
    if #actions < 2 then return actions[1] end
    local prev = actions[1]
    for i = 2, #actions do
        prev = cc.Sequence:create(prev, actions[i])
    end
    return prev;
end

function ActionMgr:getEndSpawn(actions)
	if #actions < 1 then return end
    if #actions < 2 then return actions[1] end
    local prev = actions[1]
    for i = 2, #actions do
        prev = cc.Spawn:create(prev, actions[i])
    end
    return prev;
end


mgrs.action = nil;
mgrs.singleClass(mgrs,ActionMgr,"action");
--endregion
